package com.iris.smartclass.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iris.smartclass.annotation.AuthCheck;
import com.iris.smartclass.common.BaseResponse;
import com.iris.smartclass.common.ErrorCode;
import com.iris.smartclass.common.ResultUtils;
import com.iris.smartclass.exception.BusinessException;
import com.iris.smartclass.exception.ThrowUtils;
import com.iris.smartclass.mapper.DutyMapper;
import com.iris.smartclass.model.dto.duty.*;
import com.iris.smartclass.model.entity.Duty;
import com.iris.smartclass.model.entity.User;
import com.iris.smartclass.model.vo.DutyVO;
import com.iris.smartclass.service.DutyService;
import com.iris.smartclass.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/duty")
@Slf4j
public class DutyController {

    @Resource
    private DutyService dutyService;

    @Resource
    private DutyMapper dutyMapper;

    @Resource
    private UserService userService;


    @PostMapping("/add")
    @AuthCheck(mustRole = "teacher")
    public BaseResponse<Long> addDuty(@RequestBody DutyAddRequest dutyAddRequest, HttpServletRequest request) {
        if (dutyAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Long uuid = loginUser.getId();
        Duty duty = new Duty();
        BeanUtils.copyProperties(dutyAddRequest, duty);
        duty.setUuid(uuid);
        boolean result = dutyService.save(duty);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(duty.getId());
    }

    // /**
    //  * 删除学生
    //  *
    //  * @param deleteRequest
    //  * @param request
    //  * @return
    //  */
    @PostMapping("/delete")
    @AuthCheck(mustRole = "teacher")
    public BaseResponse<Boolean> deleteDuty(@RequestBody DutyDeleteRequest dutyDeleteRequest, HttpServletRequest request) {
        QueryWrapper<Duty> deleteRequest = new QueryWrapper<>();
        deleteRequest.eq("stId", dutyDeleteRequest.getStId());
        int delete = dutyMapper.delete(deleteRequest);
        return ResultUtils.success(delete>0);
    }
    //
    // /**
    //  * 更新学生
    //  *
    //  * @param dutyUpdateRequest
    //  * @param request
    //  * @return
    //  */
    // @PostMapping("/update")
    // public BaseResponse<Boolean> updateDuty(@RequestBody DutyUpdateRequest dutyUpdateRequest,
    //                                            HttpServletRequest request) {
    //     if (dutyUpdateRequest == null || dutyUpdateRequest.getId() == null) {
    //         throw new BusinessException(ErrorCode.PARAMS_ERROR);
    //     }
    //     Duty duty = new Duty();
    //     BeanUtils.copyProperties(dutyUpdateRequest, duty);
    //     boolean result = dutyService.updateById(duty);
    //     ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
    //     return ResultUtils.success(true);
    // }
    //
    //
    //
    // /**
    //  * 分页获取学生列表（仅管理员）
    //  *
    //  * @param dutyQueryRequest
    //  * @param request
    //  * @return
    //  */
    @PostMapping("/list/page")
    public BaseResponse<Page<Duty>> listDutyByPage(@RequestBody DutyQueryRequest dutyQueryRequest,
                                                   HttpServletRequest request) {
        long current = dutyQueryRequest.getCurrent();
        long size = dutyQueryRequest.getPageSize();
        Page<Duty> dutyPage = dutyService.page(new Page<>(current, size),
                dutyService.getQueryWrapper(dutyQueryRequest,request));
        return ResultUtils.success(dutyPage);
    }
    //
    // /**
    //  * 分页获取学生封装列表
    //  *
    //  * @param dutyQueryRequest
    //  * @param request
    //  * @return
    //  */

    @PostMapping("/getDay/list")
    public BaseResponse<List<DutyVO>> getDutyVOListByDay(@RequestBody DayRequest dayRequest, HttpServletRequest request){
        List<DutyVO> dutyVOList = dutyService.getDutyVOList(dayRequest.getDay(), request);
        return ResultUtils.success(dutyVOList);
    }


    @GetMapping("/get/vo")
    public BaseResponse<DutyVO> getDutyVOById(long stId, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long uuid = loginUser.getId();
        return ResultUtils.success(dutyService.getDutyVOByID(stId,uuid));
    }
    @PostMapping("/update")
    @AuthCheck(mustRole = "teacher")
    public BaseResponse<Boolean> updateDuty(@RequestBody DutyUpdateRequest dutyUpdateRequest,
                                            HttpServletRequest request) {
        if (dutyUpdateRequest == null || dutyUpdateRequest.getName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = dutyService.updateDutyByName(dutyUpdateRequest);
        ThrowUtils.throwIf(!b, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }
}
